<template>
    <div>
        <van-nav-bar title="详情" left-text="返回" left-arrow @click-left="() => { $router.push('/goods') }" />
            <!-- 骨架屏 -->
        <van-skeleton v-if="pd">
            <template #template>
                <van-skeleton-image />
            </template>
        </van-skeleton>
        <swipe v-else :images="detail.pics"></swipe>
        <div v-html="detail.goods_introduce"></div>
        <van-action-bar>
            <van-action-bar-icon icon="chat-o" text="客服" dot />
            <van-action-bar-icon icon="cart-o" text="购物车" badge="5" />
            <van-action-bar-icon icon="shop-o" text="店铺" badge="12" />
            <van-action-bar-button type="warning" text="加入购物车" @click="addcart" />
            <van-action-bar-button type="danger" text="立即购买" />
        </van-action-bar>

    </div>
</template>

<script setup>
import { ref } from 'vue'
import { detailApi } from '../request/api'
import { showToast } from 'vant'
import swipe from '../components/swipe.vue'
import { useCounterStore } from '../store/storeCart'
// 可以在组件中的任意位置访问 `store` 变量 ✨
const store = useCounterStore()
import { useRoute, useRouter } from 'vue-router'
const router = new useRouter()
const route = new useRoute()
let pd=ref(true)
const detail = ref({})
detailApi(route.params.goods_id).then(res => {
    setTimeout(()=>{
        console.log('详情', res)
    detail.value = res.data.message
    pd=false
    },500)
    
})
//加入购物车的逻辑
const addcart = () => {
    let token = localStorage.getItem('token')
    if (!token) {
        showToast({
            type: "fail",
            message: '请先登录',
            duration: 1500,
            onClose() {
                router.push('/login')
            }
        })
    } else {
        store.addcart(detail.value, route.params.goods_id)
        router.push('/home/cart')
    }
}
</script>

<style lang="scss" scoped></style>